package com.zycfc.zsf.boot.office.pdf.generator;

import com.itextpdf.text.pdf.*;
import org.apache.commons.lang3.*;
import java.util.*;
import java.net.*;
import com.itextpdf.tool.xml.html.*;
import com.itextpdf.tool.xml.net.*;
import com.itextpdf.tool.xml.pipeline.end.*;
import com.itextpdf.tool.xml.pipeline.html.*;
import com.itextpdf.tool.xml.*;
import java.nio.charset.*;
import com.itextpdf.tool.xml.parser.*;
import java.io.*;
import com.itextpdf.tool.xml.pipeline.css.*;
import com.itextpdf.text.*;
import org.slf4j.*;

public class PdfGenerator
{
    private static final Logger log;
    
    public static void generatePlus(final String htmlStr, final OutputStream out) throws IOException, DocumentException {
        final String charsetName = "UTF-8";
        final Document document = new Document(PageSize.A4, 30.0f, 30.0f, 30.0f, 30.0f);
        document.setMargins(30.0f, 30.0f, 30.0f, 30.0f);
        final PdfWriter writer = PdfWriter.getInstance(document, out);
        document.open();
        final HashMap<String, String> fontSubstitutionMap = new HashMap<String, String>();
        fontSubstitutionMap.put("SimSun", "/font/SIMSUN.TTC,0");
        final HtmlPipelineContext htmlContext = new HtmlPipelineContext(new CssAppliersImpl((FontProvider)new XMLWorkerFontProvider("font/SIMSUN.TTC,0", fontSubstitutionMap) {
            public Font getFont(String fontname, final String encoding, final float size, final int style) {
                if (fontname == null) {
                    fontname = "SimSun";
                }
                return super.getFont(fontname, encoding, size, style);
            }
        })) {
            public HtmlPipelineContext clone() throws CloneNotSupportedException {
                final HtmlPipelineContext context = super.clone();
                try {
                    final ImageProvider imageProvider = this.getImageProvider();
                    context.setImageProvider(imageProvider);
                }
                catch (NoImageProviderException ex) {}
                return context;
            }
        };
        htmlContext.setImageProvider((ImageProvider)new AbstractImageProvider() {
            String rootPath = PdfGenerator.class.getResource("/").getPath();
            
            public String getImageRootPath() {
                return this.rootPath;
            }
            
            public Image retrieve(final String src) {
                if (StringUtils.isEmpty((CharSequence)src)) {
                    return null;
                }
                InputStream in = null;
                HttpURLConnection conn = null;
                try {
                    final String matchPath = "/images/" + src;
                    try {
                        this.rootPath = PdfGenerator.class.getResource(matchPath).getPath();
                        final List<URL> list = ResourceUtil.getFileUrls(this.rootPath, null, true, matchPath);
                        in = list.get(0).openStream();
                    }
                    catch (NullPointerException e6) {
                        final URL url = new URL(src);
                        conn = (HttpURLConnection)url.openConnection();
                        conn.setRequestMethod("GET");
                        conn.connect();
                        in = conn.getInputStream();
                    }
                    final ByteArrayOutputStream output = new ByteArrayOutputStream();
                    final byte[] buffer = new byte[4096];
                    int n = 0;
                    while (-1 != (n = in.read(buffer))) {
                        output.write(buffer, 0, n);
                    }
                    final byte[] bs = output.toByteArray();
                    final Image image = Image.getInstance(bs);
                    image.setAbsolutePosition(400.0f, 400.0f);
                    if (image != null) {
                        this.store(src, image);
                        return image;
                    }
                }
                catch (Throwable e) {
                    PdfGenerator.log.error("\u7ec4\u4ef6\u751f\u6210pdf\u4e2d\u7684\u56fe\u7247\u5931\u8d25\uff1a", e);
                    if (in != null) {
                        try {
                            in.close();
                        }
                        catch (IOException e2) {
                            PdfGenerator.log.error("\u5173\u95edpdf\u751f\u6210\u7ec4\u4ef6\u4e2d\u7684\u56fe\u7247\u6d41\u5931\u8d25\uff1a", (Throwable)e2);
                        }
                    }
                    if (conn != null) {
                        try {
                            conn.disconnect();
                        }
                        catch (Exception e3) {
                            PdfGenerator.log.error("\u5173\u95edpdf\u751f\u6210\u7ec4\u4ef6\u4e2d\u7684\u8fdc\u7a0b\u56fe\u7247\u6d41\u5931\u8d25\uff1a", (Throwable)e3);
                        }
                    }
                }
                finally {
                    if (in != null) {
                        try {
                            in.close();
                        }
                        catch (IOException e4) {
                            PdfGenerator.log.error("\u5173\u95edpdf\u751f\u6210\u7ec4\u4ef6\u4e2d\u7684\u56fe\u7247\u6d41\u5931\u8d25\uff1a", (Throwable)e4);
                        }
                    }
                    if (conn != null) {
                        try {
                            conn.disconnect();
                        }
                        catch (Exception e5) {
                            PdfGenerator.log.error("\u5173\u95edpdf\u751f\u6210\u7ec4\u4ef6\u4e2d\u7684\u8fdc\u7a0b\u56fe\u7247\u6d41\u5931\u8d25\uff1a", (Throwable)e5);
                        }
                    }
                }
                return super.retrieve(src);
            }
        });
        htmlContext.setAcceptUnknown(true).autoBookmark(true).setTagFactory(Tags.getHtmlTagProcessorFactory());
        final CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
        cssResolver.setFileRetrieve((FileRetrieve)new FileRetrieve() {
            public void processFromStream(final InputStream in, final ReadingProcessor processor) throws IOException {
                try (final InputStreamReader reader = new InputStreamReader(in, "UTF-8")) {
                    int i = -1;
                    while (-1 != (i = reader.read())) {
                        processor.process(i);
                    }
                }
                catch (Throwable t4) {}
            }
            
            public void processFromHref(final String href, final ReadingProcessor processor) throws IOException {
                final InputStream is = PdfGenerator.class.getResourceAsStream("/" + href);
                try (final InputStreamReader reader = new InputStreamReader(is, "UTF-8")) {
                    int i = -1;
                    while (-1 != (i = reader.read())) {
                        processor.process(i);
                    }
                }
                catch (Throwable e) {
                    e.printStackTrace();
                }
            }
        });
        final HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, (Pipeline)new PdfWriterPipeline(document, writer));
        final Pipeline<?> pipeline = (Pipeline<?>)new CssResolverPipeline(cssResolver, (Pipeline)htmlPipeline);
        XMLWorker worker = null;
        worker = new XMLWorker((Pipeline)pipeline, true);
        final XMLParser parser = new XMLParser(true, (XMLParserListener)worker, Charset.forName("UTF-8"));
        try (final InputStream inputStream = new ByteArrayInputStream(htmlStr.getBytes())) {
            parser.parse(inputStream, Charset.forName("UTF-8"));
        }
        document.close();
    }
    
    static {
        log = LoggerFactory.getLogger((Class)PdfGenerator.class);
    }
}
